/**
 * @author yanggjun
 */

Explosion = {
	particleSystem : '',
	particleCount :1800,
	particleList: [],
	particles: '',
	pos:{x:0,y:0,z:0},
	times: 10,
	startExplode: false,
	vocity: 10,
	pMaterial:'',
	initParticle : function(){

		    this.particles = new THREE.Geometry(),
		    this.pMaterial =
		      new THREE.ParticleBasicMaterial({
		        color: 0xFF0000,
		        size: 30,
		        transparent: true,
		        opacity: 1,
		      });

		// now create the individual particles
		for(var p = 0; p < this.particleCount; p++) {

		  // create a particle with random
		  // position values, -250 -> 250
		  var pX = 0,
		      pY = 500,
		      pZ = 0,
		  particle = new THREE.Vector3(pX, pY, pZ);
		 
		  var item= new Object();
		  item.direction = {};
		  item.pos = {};
		  var roty = Math.random()*Math.PI*2;
		  var rotxz = Math.random()*Math.PI*2;
		  item.direction.x = Math.cos(roty)*Math.cos(rotxz);
		  item.direction.y = Math.sin(rotxz);
		  item.direction.z = Math.sin(roty)*Math.cos(rotxz);
		  item.pos.x = item.pos.y = item.z = -100;
		  this.particleList.push(item);
		  

		  // add it to the geometry
		  this.particles.vertices.push(particle);
		}

		// create the particle system
		this.particleSystem =
		  new THREE.ParticleSystem(
		    this.particles,
		    this.pMaterial);
		
		 this.particleSystem.sortParticles = true;
		// add it to the scene
		World.scene.add(this.particleSystem);


	},
	
	setExplosionPos : function(x,y,z,times){
		this.times = times;
		for(var p = 0; p < this.particleCount; p++){
			var particle = this.particles.vertices[p];
			particle.set(x,y,z);
			this.particleList[p].pos.x = x;
			this.particleList[p].pos.y = y;
			this.particleList[p].pos.z = z;
		}
		this.startExplode = true;
		this.particleSystem.geometry.__dirtyVertices = true;
	},
	
	explode: function(){
		if(this.startExplode && this.times < 300){
			this.times += 50;
			this.vocity += 80
		for(var i=0; i<this.particleCount; i++){
			 this.particleList[i].pos.x = this.particleList[i].pos.x + this.vocity*this.particleList[i].direction.x;
			 this.particleList[i].pos.y = this.particleList[i].pos.y + this.vocity*this.particleList[i].direction.y;
			 this.particleList[i].pos.z = this.particleList[i].pos.z + this.vocity*this.particleList[i].direction.z;
			
			var particle = this.particles.vertices[i];
			particle.set(this.particleList[i].pos.x,  this.particleList[i].pos.y,  this.particleList[i].pos.z);

			this.particleSystem.geometry.__dirtyVertices = true;

		}
		}else{
			this.times = 10;
			this.vocity = 10;
			this.startExplode = false;
			this.pMaterial.opacity = 1;
			for(var i=0; i<this.particleCount; i++){
				var particle = this.particles.vertices[i];
				particle.set(-100,-100,-100);
			}
			
		}
	}
};
